package com.czkt.config;

import com.czkt.entity.SysRight;
import com.czkt.service.ISysRightService;
import com.czkt.service.ISysRoleService;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Resource
    ISysRightService sysRightService;


    public RedisManager redisManager(){
        RedisManager redisManager = new RedisManager();
        return redisManager;
    }

    public RedisCacheManager cacheManager(){
        RedisCacheManager cacheManager = new RedisCacheManager();
        cacheManager.setRedisManager(redisManager());
        //缓存名称
        cacheManager.setPrincipalIdFieldName("roleId");
        //缓存有效时间
        cacheManager.setExpire(600);
        return cacheManager;
    }

    //会话操作
    public RedisSessionDAO redisSessionDAO(){
        RedisSessionDAO sessionDAO = new RedisSessionDAO();
        sessionDAO.setRedisManager(redisManager());
        return sessionDAO;
    }

    //会话管理
    public DefaultWebSessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionDAO(redisSessionDAO());
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }

    //加密
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {

        // 设置加密算法
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("md5");
        // 设置加密次数
        credentialsMatcher.setHashIterations(2);

        return credentialsMatcher;
    }



    @Bean
    public MyShiroRealm myShiroRealm(){
        MyShiroRealm shiroRealm = new MyShiroRealm();
        //设置启用缓存，并设置缓存名称
//        shiroRealm.setCachingEnabled(true);
//        shiroRealm.setAuthenticationCachingEnabled(true);
        shiroRealm.setAuthorizationCachingEnabled(true);
//        shiroRealm.setAuthenticationCacheName("authenticationCache");
        shiroRealm.setAuthorizationCacheName("authorizationCache");
        shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return shiroRealm;
    }


    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //注入Realm
        securityManager.setRealm(myShiroRealm());
        //注入缓存管理器
        securityManager.setCacheManager(cacheManager());
        //注入会话管理器
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){//Shiro过滤器：权限验证
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        // 注册安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager());


        /*
         * 设置登录页面的地址
         * 当用户访问认证资源的时候，如果用户没有登录，那么就会跳转到该属性指定的页面
         */

        Map<String,String> filterChainDefinitonMap = new LinkedHashMap<>();
        filterChainDefinitonMap.put("/css/*","anon");
        filterChainDefinitonMap.put("/fonts/*","anon");
        filterChainDefinitonMap.put("/images/*","anon");
        filterChainDefinitonMap.put("/js/*","anon");
        filterChainDefinitonMap.put("/localcss/*","anon");
        filterChainDefinitonMap.put("/localjs/*","anon");
        filterChainDefinitonMap.put("/login","anon");
        filterChainDefinitonMap.put("/doLogin","anon");
        filterChainDefinitonMap.put("/logout","anon");


        List<SysRight> rightList = sysRightService.list();
        for (SysRight sysRight : rightList) {
            if(StringUtils.hasText(sysRight.getRightUrl())){
                filterChainDefinitonMap.put(sysRight.getRightUrl(),"perms["+sysRight.getRightCode()+"]");
            }
        }


        filterChainDefinitonMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitonMap);
        shiroFilterFactoryBean.setLoginUrl("/login");
        return shiroFilterFactoryBean;
    }
}
